import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 布局组件
const Layout = () => import('@/views/layout/Index')

// 路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/Index'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    name: 'Dashboard',
    meta: { title: '仪表盘', icon: 'el-icon-s-data' },
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/Index'),
        name: 'Dashboard',
        meta: { title: '系统概况', icon: 'el-icon-s-data' }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    name: 'System',
    meta: { title: '系统监控', icon: 'el-icon-monitor' },
    children: [
      {
        path: 'info',
        component: () => import('@/views/system/Info'),
        name: 'SystemInfo',
        meta: { title: '系统信息', icon: 'el-icon-info' }
      },
      {
        path: 'processes',
        component: () => import('@/views/system/Processes'),
        name: 'SystemProcesses',
        meta: { title: '进程管理', icon: 'el-icon-s-operation' }
      },
      {
        path: 'services',
        component: () => import('@/views/system/Services'),
        name: 'SystemServices',
        meta: { title: '服务管理', icon: 'el-icon-s-goods' }
      }
    ]
  },
  {
    path: '/file',
    component: Layout,
    name: 'File',
    meta: { title: '文件管理', icon: 'el-icon-folder' },
    children: [
      {
        path: 'manager',
        component: () => import('@/views/file/Manager'),
        name: 'FileManager',
        meta: { title: '文件管理器', icon: 'el-icon-document' }
      }
    ]
  },
  {
    path: '/nginx',
    component: Layout,
    name: 'Nginx',
    meta: { title: 'Web站点', icon: 'el-icon-s-platform' },
    children: [
      {
        path: 'sites',
        component: () => import('@/views/nginx/Sites'),
        name: 'NginxSites',
        meta: { title: '站点管理', icon: 'el-icon-s-flag' }
      },
      {
        path: 'config',
        component: () => import('@/views/nginx/Config'),
        name: 'NginxConfig',
        meta: { title: 'Nginx配置', icon: 'el-icon-setting' }
      }
    ]
  },
  {
    path: '/database',
    component: Layout,
    name: 'Database',
    meta: { title: '数据库', icon: 'el-icon-s-management' },
    children: [
      {
        path: 'mysql',
        component: () => import('@/views/database/MySQL'),
        name: 'MySQL',
        meta: { title: 'MySQL管理', icon: 'el-icon-s-order' }
      },
      {
        path: 'mongodb',
        component: () => import('@/views/database/MongoDB'),
        name: 'MongoDB',
        meta: { title: 'MongoDB管理', icon: 'el-icon-s-order' }
      }
    ]
  },
  {
    path: '/security',
    component: Layout,
    name: 'Security',
    meta: { title: '安全设置', icon: 'el-icon-s-claim' },
    children: [
      {
        path: 'firewall',
        component: () => import('@/views/security/Firewall'),
        name: 'Firewall',
        meta: { title: '防火墙', icon: 'el-icon-s-finance' }
      },
      {
        path: 'ssh',
        component: () => import('@/views/security/SSH'),
        name: 'SSH',
        meta: { title: 'SSH设置', icon: 'el-icon-s-custom' }
      },
      {
        path: 'fail2ban',
        component: () => import('@/views/security/Fail2ban'),
        name: 'Fail2ban',
        meta: { title: 'Fail2ban', icon: 'el-icon-s-release' }
      }
    ]
  },
  {
    path: '/task',
    component: Layout,
    name: 'Task',
    meta: { title: '定时任务', icon: 'el-icon-alarm-clock' },
    children: [
      {
        path: 'crontab',
        component: () => import('@/views/task/Crontab'),
        name: 'Crontab',
        meta: { title: 'Crontab管理', icon: 'el-icon-time' }
      }
    ]
  },
  {
    path: '/app',
    component: Layout,
    name: 'App',
    meta: { title: '应用商店', icon: 'el-icon-shopping-cart-full' },
    children: [
      {
        path: 'store',
        component: () => import('@/views/app/Store'),
        name: 'AppStore',
        meta: { title: '应用列表', icon: 'el-icon-s-shop' }
      },
      {
        path: 'installed',
        component: () => import('@/views/app/Installed'),
        name: 'AppInstalled',
        meta: { title: '已安装应用', icon: 'el-icon-s-fold' }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    name: 'User',
    meta: { title: '用户管理', icon: 'el-icon-user', roles: ['admin'] },
    children: [
      {
        path: 'list',
        component: () => import('@/views/user/List'),
        name: 'UserList',
        meta: { title: '用户列表', icon: 'el-icon-user-solid' }
      },
      {
        path: 'profile',
        component: () => import('@/views/user/Profile'),
        name: 'UserProfile',
        meta: { title: '个人设置', icon: 'el-icon-s-tools' }
      }
    ]
  },
  // 404页面必须放在最后
  {
    path: '/404',
    component: () => import('@/views/404/Index'),
    hidden: true
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// 路由守卫
router.beforeEach((to, from, next) => {
  // 判断是否需要登录
  const token = localStorage.getItem('token')
  
  if (to.path === '/login') {
    // 如果是访问登录页，有token则跳转到首页
    if (token) {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    // 其他页面，没有token则跳转到登录页
    if (!token) {
      next({ path: '/login' })
    } else {
      next()
    }
  }
})

export default router 